//
//  CouponGiveVC.m
//  GasBusiness
//
//  Created by zfc on 2021/8/1.
//

#import "CouponGiveVC.h"

#import "CouponManagerCell.h"
#import "CouponGiveCell.h"

@interface CouponGiveVC ()

@property (nonatomic, strong) UITextField *phoneTf;
@property (nonatomic, strong) UITextField *countTf;

@end

@implementation CouponGiveVC

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = @"优惠券赠送";
    [self configTableView];
}

- (void) configTableView {
    self.isRemoveHeader = YES;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-kBottomWhiteHeight);
    }];
    [self.tableView registerClass:[CouponManagerCell class] forCellReuseIdentifier:@"CouponManagerCell"];
    [self.tableView registerClass:[CouponGiveCell class] forCellReuseIdentifier:@"CouponGiveCell"];

}

- (void) giveAction {

    BOOL isValid = [Untils cheackPhoneNumValid:self.phoneTf.text];
    if(!isValid){
        [SVProgressHUD showInfoWithStatus:@"请输入正确的手机号码"];
        return;
    }

    if (self.countTf.text.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入赠送数量"];
        return;
    }
    NSString *url = @"/tiny-shop/v1/marketing/coupon-type/give";
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"mobile"] = self.phoneTf.text;
    params[@"num"] = self.countTf.text;
    params[@"coupon_type_id"] = self.dic[@"id"];
    [[NetWorksTools shared] requestWithMethod:POST url:url params:params successBlock:^(NSDictionary * _Nonnull response) {
        [SVProgressHUD showInfoWithStatus:@"赠送成功"];
        [self.navigationController popViewControllerAnimated:YES];
    } failureBlock:^(NSError * _Nonnull error) {

    }];
}

#pragma mark tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.section == 0) {
        CouponManagerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CouponManagerCell"];
        cell.givebtn.hidden = YES;
        cell.dic = self.dic;
        return cell;
    } else {
        CouponGiveCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CouponGiveCell"];
        self.phoneTf = cell.phoneTf;
        self.countTf = cell.countTf;
        WeakSelf;
        cell.givebtn.block = ^(UIButton *sender) {
            [weakSelf giveAction];
        };
        return cell;
    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 126;
    } else {
        return UITableViewAutomaticDimension;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = ColorF5;
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = ColorF5;
    return view;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

@end
